package com.yang.dao.impl;

import com.yang.dao.CheckInDao;
import com.yang.entity.*;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Repository
public class CheckInDaoImpl implements CheckInDao {

    public static Logger logger = Logger.getLogger(CheckInDaoImpl.class);

    @Autowired
    HibernateTemplate hibernateTemplate;
    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<RateEntity> selectMoneyByroomNum(String roomNum) {
        String sql = "SELECT * FROM rate WHERE id in (SELECT money_id FROM room WHERE room_num = ?) ";
        ResultSetExtractor<List<RateEntity>> lists = new ResultSetExtractor<List<RateEntity>>() {
            @Override
            public List<RateEntity> extractData(ResultSet rs) throws SQLException, DataAccessException {
                List<RateEntity> lists = new ArrayList<RateEntity>();
                while(rs.next()) {
                    RateEntity rateEntity = new RateEntity();
                    rateEntity.setId(rs.getLong("id"));
                    rateEntity.setMoney(rs.getFloat("money"));
                    rateEntity.setPrepay(rs.getFloat("prepay"));
                    lists.add(rateEntity);
                }
                return lists;
            }
        };
        return jdbcTemplate.query(sql,new Object[] {roomNum},lists);
    }

    @Override
    public RateEntity selectRateById(long rateId) {
        Session session = sessionFactory.openSession();

        Criteria criteria = session.createCriteria(RateEntity.class);
        criteria.add(Restrictions.eq("id",rateId));
        RateEntity rateEntity = (RateEntity) criteria.uniqueResult();
        session.close();
        return rateEntity;
    }

    @Override
    public boolean saveGuest(GuestEntity guestEntity) {
        try{
            hibernateTemplate.save(guestEntity);
        }catch (DataAccessException e){
            e.printStackTrace();
            logger.error("顾客保存出错");
            return false;
        }
        return true;
    }

    @Override
    public boolean updataRoom(RoomEntity roomEntity) {
        try{
            hibernateTemplate.update(roomEntity);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("更新房间出错");
            return false;
        }
        return true;
    }

    @Override
    public boolean saveOrderForm(OrderFormEntity orderFormEntity) {
        try {
            hibernateTemplate.save(orderFormEntity);
        }catch (DataAccessException e){
            e.printStackTrace();
            logger.error("保存订单出错！");
            return false;
        }
        return true;
    }

    @Override
    public boolean saveLiverRelation(LiverRelationEntity liverRelationEntity) {
        try{
            hibernateTemplate.save(liverRelationEntity);
        }catch (DataAccessException e){
            e.printStackTrace();
            logger.error("保存居住关系出错");
            return false;
        }
        return true;
    }
}
